<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组件</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<style>
    .regbg{
        background-color: #1D6911;
        height: 200px;
        width: 200px;
    }
</style>
<body>
<div class="div1">
    <button-count></button-count>
    <button @click="changeComponent">改变</button>
    <keep-alive>
        <component :is="who">
        </component>
    </keep-alive>
    <div v-text="msg"></div>
</div>

<template id="tem">
    <div>
        <button @click="count++">erha点击了{{count}}次</button>
        <ul>
            <li v-for="value in arr">{{value}}</li>
        </ul>
    </div>
</template>
</body>
<script>


    Vue.component("button-count",{
        data:function () {
            return{
                count:0,
                arr:["5","1","7"]
            }
        },
        template:"#tem"
    },);


    new Vue({
        el:".div1",
        data:{
            who:"my-p"
        },
        components:{
            "my-div":{
                data:function () {
                    return{
                        age:Math.random()
                    }
                },
                template:`<div class="regbg">{{age}}</div>`
            },
            "my-p":{
                data:function () {
                    return{
                        age:Math.random()
                    }
                },
                template:`<p class="redbg">sasadas,{{age}}</p>`
            }
        },
        methods:{
            changeComponent:function () {
                if(this.who=='my-p'){
                    this.who='my-div'
                }else {
                    this.who='my-p'
                }
            }
        }
    })
</script>
</html>
